package com.ac.input.web.action.imp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.jdbc.core.JdbcTemplate;

import com.caac.core.web.action.BaseAction;
import com.caac.core.web.form.BaseForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:project@agilecentury.com.cn">Project Group</a>
 * @version 1.0
 */

public class ImpStep3Action extends BaseAction {
    protected ActionForward doExecute(ActionMapping mapping, BaseForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
        String[] keyRules = request.getParameterValues("keyRules");
        String[] aimRules = request.getParameterValues("aimRules");
        String fromCode = request.getParameter("fromCode");
        String toCode = request.getParameter("toCode");
        JdbcTemplate jt = this.getJdbcTemplate();
        
        String  sql = "SELECT TAB_NAME FROM HND_INP_DEF WHERE INP_CODE='" + toCode + "'";
        String aName = (String)jt.queryForObject(sql, String.class);
        sql = "SELECT TAB_NAME FROM HND_INP_DEF WHERE INP_CODE='" + fromCode + "'";
        String bName = (String)jt.queryForObject(sql, String.class);
        String fields = ""; 
        String values = ""; 
        String valueUpdates = ""; 
               
        if (aimRules != null) {
	       for (int i = 0; i < aimRules.length; i++) {
	        	String tmp = aimRules[i];
	        	String[] rule =  tmp.split("=");
	        	fields += rule[0]+",";
	        	String[] vNames = rule[1].split("\\|\\|"); 
	        	String value = "";
	        	valueUpdates += rule[1]+",";
	        	for(int j=0; j < vNames.length; j++){
	        		if(vNames[j].indexOf("A.")<0){
	        			value = (value.equals(""))?vNames[j]:(value+"||"+vNames[j]);
	        		}
	        	}
	        	values += value+",";
	        }
        }
        
        String updateWhere ="";
        if (keyRules != null) {
            for (int i = 0; i < keyRules.length; i++) {
            	updateWhere += " AND " + keyRules[i];
            }
        }
        
        String insertSql ="INSERT INTO "+ aName + " A ("+fields+"A.ID) (SELECT "+values+"B.ID FROM "+ bName
    	    + " B WHERE NOT EXISTS(SELECT 1 FROM "+ aName + " A WHERE 1=1 "+ updateWhere+"))";
        
        String updateSql ="UPDATE "+aName+" A SET("+fields.substring(0,fields.length()-1)+")=(SELECT "
            + valueUpdates.substring(0,valueUpdates.length()-1)+" FROM "+ bName 
            + " B WHERE 1=1 "+ updateWhere
            + " ) WHERE EXISTS(SELECT 1 FROM "+bName+" B WHERE 1=1 "+ updateWhere+")";
       
        log.debug(updateSql);
        log.debug(insertSql);
        
        jt.update(updateSql);
        jt.update(insertSql);
        
		return forward(mapping);
	}
}
